#! /usr/bin/env python
#coding=utf-8
from uliweb import expose
from sqlalchemy import *
from uliweb.orm import *
from uliweb.utils.pyini import Ini
from sqlalchemy.sql import select
from sqlalchemy.sql import and_, or_, not_
import time, sys
sys.path.insert(0, '../apps')
from departments.models import *
#from requirements.models import *
#from charts.models import *
#from charts.models import *
import datetime
from datetime import date
from get_month_quarter import *
from get_assignment_report_detail import *
#from __future__ import division

#db = get_connection('sqlite:///../database.db', debug=False)
#调用get_month_quarter.py中get_db_connection()获取数据库联接

#from uliweb.core.SimpleFrame import Dispatcher
#settings = {'ORM/DEBUG_LOG':True, 'ORM/AUTO_CREATE':False,'ORM/CONNECTION':'sqlite:///../database.db'}
#app = Dispatcher(apps_dir='../apps', start=False, default_settings=settings)

db = get_db_connection()

#meta = MetaData(db)
#db.metadata.reflect()


def run_ass_week_tx():
    
    req_model = get_model('assignment_week_report')
    first_day,last_day = get_week()
    month_first_day = first_day
    month_last_day = last_day
    report_type = '1'
    id_type = '1'
    data = get_assignment_week_report(month_first_day,month_last_day,report_type,id_type)
    req_model.remove(and_(req_model.c.report_date == month_last_day,
                            req_model.c.report_type == report_type,
                            req_model.c.id_type == id_type))
    for v,t in data.items():
        #print 't',t,'v',v
        for x,y in t.items():
            r = req_model(**y)
            r.save()

if __name__ == '__main__':
    try:
        db.begin()
        run_ass_week_tx()
        db.commit()
        log.info("finish")
    except Exception, e:
        db.rollback()
        log.exception(e)
        log.error('error')
        raise


def run_ass_week_kfzx():
    
    req_model = get_model('assignment_week_report')
    first_day,last_day = get_week()
    month_first_day = first_day
    month_last_day = last_day
    report_type = '1'
    id_type = '2'
    data = get_assignment_week_report_kfzx(month_first_day,month_last_day,report_type,id_type)
    req_model.remove(and_(req_model.c.report_date == month_last_day,
                            req_model.c.report_type == report_type,
                            req_model.c.id_type == id_type))
    for v,t in data.items():
        #print 't',t,'v',v
        for x,y in t.items():
            r = req_model(**y)
            r.save()

if __name__ == '__main__':
    try:
        db.begin()
        run_ass_week_kfzx()
        db.commit()
        log.info("finish")
    except Exception, e:
        db.rollback()
        log.exception(e)
        log.error('error')
        raise


def run_ass_week_system():
    
    req_model = get_model('assignment_week_report')
    first_day,last_day = get_week()
    month_first_day = first_day
    month_last_day = last_day
    report_type = '1'
    id_type = '3'
    data = get_assignment_week_report(month_first_day,month_last_day,report_type,id_type)
    req_model.remove(and_(req_model.c.report_date == month_last_day,
                            req_model.c.report_type == report_type,
                            req_model.c.id_type == id_type))
    for v,t in data.items():
        #print 't',t,'v',v
        for x,y in t.items():
            r = req_model(**y)
            r.save()

if __name__ == '__main__':
    try:
        db.begin()
        run_ass_week_system()
        db.commit()
        log.info("finish")
    except Exception, e:
        db.rollback()
        log.exception(e)
        log.error('error')
        raise

